/**
 * 根据用户权限过滤路由
 * @param route - 权限路由
 * @param roles - 权限
 */
function hasPermission(route, role) {
  return !route.meta?.roles || (route.meta?.roles || []).includes(role);
}

/** 递归过滤异步路由表，筛选角色权限路由
 * @param routes - 权限路由组
 * @param roles - 权限
 */
export function filterRoutesByRole(routes, role) {
  const res = [];
  routes.forEach(route => {
    const tmp = { ...route };
    if (hasPermission(tmp, role)) {
      if (tmp.children) {
        tmp.children = filterRoutesByRole(tmp.children, role);
      }
      res.push(tmp);
    }
  });
  return res;
}
